package com.white.commonlib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.white.commonlib.R;

/**
 * Created by yutf on 2017/6/20 0020.
 */

public class SettingItem extends RelativeLayout
{
    private int drawablePadding = 8;

    private @DrawableRes
    int itemLeftIcon;
    private @DrawableRes
    int itemRightIcon;

    private String text;
    private int textSize = 15;
    private int textColor = 0xFF989898;

    private Context mContext;

    private boolean rightTextEnable;
    private TextView rightText;
    private String rightTxtStr = "";

    private Paint dividerPaint;
    private @ColorInt int dividerColor = Color.GRAY;
    private int dividerHeight = 1;
    private boolean enableTopDivider, enableBottomDivider;

    public SettingItem(Context context)
    {
        this(context, null);
    }

    public SettingItem(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
        initAttrs(attrs);
        initContent();
    }

    private static final int[] ATTRS = new int[]{
            android.R.attr.textSize, android.R.attr.textColor,
            android.R.attr.padding, android.R.attr.text};

    private void initAttrs(AttributeSet attrs)
    {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        drawablePadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, drawablePadding, dm);
        textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, textSize, dm);

        TypedArray t = mContext.obtainStyledAttributes(attrs, R.styleable.SettingItem);
        drawablePadding = t.getDimensionPixelSize(R.styleable.SettingItem_si_drawable_padding, drawablePadding);
        itemLeftIcon = t.getResourceId(R.styleable.SettingItem_si_left_icon, 0);
        itemRightIcon = t.getResourceId(R.styleable.SettingItem_si_right_icon, 0);
        rightTextEnable = t.getBoolean(R.styleable.SettingItem_si_right_text_enable, false);
        enableTopDivider = t.getBoolean(R.styleable.SettingItem_si_enable_top_divider, false);
        enableBottomDivider = t.getBoolean(R.styleable.SettingItem_si_enable_bottom_divider, false);
        dividerHeight = t.getDimensionPixelSize(R.styleable.SettingItem_si_divider_height, 1);
        dividerColor = t.getColor(R.styleable.SettingItem_si_divider_color, dividerColor);
        t.recycle();

        TypedArray a = mContext.obtainStyledAttributes(attrs, ATTRS);
        textSize = a.getDimensionPixelSize(0, textSize);
        textColor = a.getColor(1, textColor);
        text = (String) a.getText(3);
        if (TextUtils.isEmpty(text))
        {
            text = "";
        }
        a.recycle();
    }


    private void initContent()
    {
        if(enableTopDivider || enableBottomDivider)
        {
            dividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            dividerPaint.setStyle(Paint.Style.STROKE);
            dividerPaint.setColor(dividerColor);
            dividerPaint.setStrokeWidth(dividerHeight);
            setWillNotDraw(false);
        }

        createTextView();

        setClickable(true);

        createMoreImage();
        if (rightTextEnable)
        {
            createRightText();
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas)
    {
        super.dispatchDraw(canvas);
        int height = getMeasuredHeight();
        int width = getMeasuredWidth();
        if(enableBottomDivider)
        {
            float y = height - dividerHeight * 1.0f /2;
            canvas.drawLine(0, y, width,y,dividerPaint);
        }

        if(enableTopDivider)
        {
            float y = dividerHeight * 1.0f /2;
            canvas.drawLine(0, y, width,y,dividerPaint);
        }

    }

    private void createTextView()
    {
        TextView tv = new TextView(mContext);
        tv.setText(text);
        tv.setTextColor(textColor);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        LayoutParams p = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        p.addRule(RelativeLayout.CENTER_VERTICAL);
        if (itemLeftIcon != 0)
        {
            Drawable d = getResources().getDrawable(itemLeftIcon);
            if (d != null)
            {
                d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                tv.setCompoundDrawables(d, null, null, null);
                tv.setCompoundDrawablePadding(drawablePadding);
            }
        }
        addView(tv, p);
    }

    private void createMoreImage()
    {
        if (itemRightIcon != 0)
        {
            ImageView iv = new ImageView(mContext);
            iv.setId(R.id.SI_MoreId);
            iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
            iv.setImageResource(itemRightIcon);
            LayoutParams p = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            p.addRule(RelativeLayout.CENTER_VERTICAL);
            p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            addView(iv, p);
        }
    }

    public void setRightTxtStr(String rightTxtStr)
    {
        this.rightTxtStr = rightTxtStr;
        if (rightTextEnable && rightText != null)
        {
            rightText.setText(rightTxtStr);
        }
    }

    private void createRightText()
    {
        rightText = new TextView(mContext);
        rightText.setTextColor(textColor);
        int rightSize = textSize - 5;
        rightText.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightSize > 0 ? rightSize : textSize);
        rightText.setGravity(Gravity.CENTER_VERTICAL);
        LayoutParams p = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        p.rightMargin = 25;
        //如果右边没有设置图标
        if (itemRightIcon == 0)
        {
            p.addRule(RelativeLayout.CENTER_VERTICAL);
            p.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        } else
        {
            p.addRule(RelativeLayout.CENTER_VERTICAL);
            p.addRule(RelativeLayout.LEFT_OF, R.id.SI_MoreId);
        }
        addView(rightText, p);
//        AutoUtils.autoTextSize(rightText);
    }
}
